<?php
include_once("adminBase.php");
include_once(_INC_PATH.'comment_statistic.php');

class Estimate extends AdminBase{
	function del()
	{
		$est_id = $_REQUEST['id'];
		$sql = " UPDATE	`tbl_estimate` SET del_flg = '1',delete_time=CURRENT_TIMESTAMP WHERE id ='".$est_id."'";
		$this->db->query($sql);
		
		//删除该评价的回复
		$sql = "update `tbl_response` set del_flg = '1',delete_time=CURRENT_TIMESTAMP where estimate_id ='".$est_id."'";
		$this->db->query($sql);
		
		$sql = "select show_flg from `tbl_estimate` where id='$est_id'";
		$show_flg = $this->db->loadResult($sql);
		$this->updateCache($est_id);

		$this->managerLog($cor_id, 'tbl_corporation');

		echo "ok";die;
	}

	function index()
	{
		$where = '';
		if($_REQUEST['est_id']) $where .= " and te.id='".$_REQUEST['est_id']."'";
		if($_REQUEST['cor_id']) $where .= " and te.cor_id='".$_REQUEST['cor_id']."'";
		if($_REQUEST['user_id']) $where .= " and te.user_id='".$_REQUEST['user_id']."'";
		if($_REQUEST['user_name']) $where .= " and te.user_name like '%".$_REQUEST['user_name']."%'";
		if($_REQUEST['time_1']) $where .= " and left(te.create_time,10)>='".$_REQUEST['time_1']."'";
		if($_REQUEST['time_2']) $where .= " and left(te.create_time,10)<='".$_REQUEST['time_2']."'";
		if($_REQUEST['show_flg'] != ""){
			$where .= " and show_flg in ('".implode("','",$_REQUEST['show_flg'])."')";
		}else{
			$_REQUEST['show_flg'] = '0';
			$where .= " and show_flg='".$_REQUEST['show_flg']."'";
		}
		$sql="SELECT count(*)as total 
		from `tbl_estimate` te 
		left join `tbl_user` tu on tu.user_id=te.user_id
		left join `tbl_corporation` tc on tc.id=te.cor_id
		where te.del_flg='0'".$where;
		$total = $this->db->loadResult($sql);
		$curPage = @intval($_REQUEST['p']);
		if($curPage < 1) $curPage = 1;
		$showNum = _BACKVIEWNUM;
		$pageLink = "/management/admin_estimate.php";
		$page = new PageClass($total,$curPage,$showNum,$pageLink);
		$limitStart = $page->limitStart;
		$this->smarty->assign("pageList",$page->pageList);
		
		$sql = "select te.*,(select count(tr.id) from `tbl_response` tr where tr.estimate_id=te.id)as count,(case when length(te.user_name)>0 then te.user_name else te.user_id end)as user_name,tc.name as cor_name 
		from `tbl_estimate` te 
		left join `tbl_corporation` tc on tc.id=te.cor_id
		where te.del_flg='0'".$where." order by te.create_time desc limit $limitStart,$showNum";//echo $sql;
		$this->db->setQuery($sql);
		$data = $this->db->loadList();
		foreach($data as $key=>$value)
		{
			$data[$key]['content'] = content_replace($value['content'],'','','aa');
		}
		$this->smarty->assign("row",$data);
		$this->smarty->assign("curPage",$curPage);
		$show_flg_arr = array('0'=>'未通过','1'=>'已通过');
		$this->smarty->assign("show_flg_arr",$show_flg_arr);
		$this->smarty->assign("request",requestToStr());
		adminDisplay("admin_estimate.tpl");
	}

	function auditing()
	{
		$ids = $_REQUEST['selectId'];
		if(is_array($ids) && !empty($ids))
		{
			$sql = "update tbl_estimate set show_flg='1' where id in ('".implode("','",$ids)."')";
			$this->db->query($sql);
			foreach($ids as $key=>$est_id){
				$this->updateCache($est_id);
			}
		}
		$this->index();
	}

	function delAll()
	{
		$ids = $_REQUEST['selectId'];
		if(is_array($ids) && !empty($ids))
		{
			$sql = "update tbl_estimate set del_flg='1',delete_time=CURRENT_TIMESTAMP where id in ('".implode("','",$ids)."')";
			$this->db->query($sql);
			$sql = "update `tbl_response` set del_flg = '1',delete_time=CURRENT_TIMESTAMP where estimate_id in ('".implode("','",$ids)."')";
			$this->db->query($sql);

			$this->managerLog($ids, 'tbl_estimate');

			foreach($ids as $key=>$est_id){
				$this->updateCache($est_id);
			}
		}

		$this->index();
	}

	function delCompletely()
	{
		$ids = $_REQUEST['selectId'];
		if(is_array($ids) && !empty($ids))
		{
			$sql = "delete from `tbl_estimate` where id in ('".implode("','",$ids)."')";
			$this->db->query($sql);
			$sql = "delete from `tbl_response` where estimate_id in ('".implode("','",$ids)."')";
			$this->db->query($sql);
			foreach($ids as $key=>$est_id){
				$this->updateCache($est_id);
			}
		}

		$this->index();
	}
	//更新缓存页面,评论统计
	function updateCache($est_id)
	{
		$est_id = intval($est_id);
		if($est_id > 0)
		{
			$sql = "select cor_id,show_flg from tbl_estimate where id='$est_id'";
			$data = $this->db->loadRow($sql);
			$cor_id = $data['cor_id'];
			$show_flg = $data['show_flg'];
			if($show_flg == '1'){
				unlink(_CORPORATION_PATH.$cor_id.".html");
				$statistic = new Statistic($cor_id);
				$statistic->updateStatistic();
			}
		}	
	}
}

$estimate = new Estimate();